package com.example.anpuser.controller.crud;


import com.example.anpapi.entity.po.usermodel.UserRelation.UserRelation;
import com.example.anpapi.entity.vo.R;
import com.example.anpuser.service.interfaces.UserRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user-relation")
public class UserRelationController {

    @Autowired
    private UserRelationService userRelationService;

    /**
     * 用户关系信息添加
     * @param userRelation
     * @return
     */
    @PostMapping("/save")
    public R<UserRelation> save(@RequestBody UserRelation userRelation){
        log.info("用户关系信息添加:{}", userRelation);
        userRelationService.save(userRelation);
        return R.success(userRelation,"用户关系信息添加成功");
    }

    /**
     * 根据关系id删除用户关系信息
     * @param userRelation
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody UserRelation userRelation){
        log.info("用户关系信息删除:{}", userRelation);
        if(userRelationService.getById(userRelation.getId()) != null)
        {
            userRelationService.removeById(userRelation.getId());
            return R.success(null,"用户关系信息删除成功");
        }
        return R.error("未找到该用户关系信息");
    }

    /**
     * 根据关系id查找用户关系信息
     * @param id
     * @return
     */
    @GetMapping("/get")
    public R<UserRelation> get(@RequestParam(value = "id") Long id) {
        log.info("查询用户关系信息的id为{}", id);
        UserRelation userRelation = userRelationService.getById(id);
        return R.success(userRelation, "查询用户关系信息成功");
    }

    /**
     * 根据关注者id查找用户关系信息
     * @param followerId
     * @return
     */
    @GetMapping("/getByFollowerId")
    public R<UserRelation> getByFollowerId(@RequestParam(value = "followerId") Long followerId) {
        log.info("查询用户关系信息的followerId为{}", followerId);
        UserRelation userRelation = userRelationService.lambdaQuery()
                .eq(UserRelation::getFollowerId, followerId)
                .one();
        return R.success(userRelation, "查询用户关系信息成功");
    }

    /**
     * 根据被关注者id查找用户关系信息
     * @param followingId
     * @return
     */
    @GetMapping("/getByFollowingId")
    public R<UserRelation> getByFollowingId(@RequestParam(value = "followingId") Long followingId) {
        log.info("查询用户关系信息的followingId为{}", followingId);
        UserRelation userRelation = userRelationService.lambdaQuery()
                .eq(UserRelation::getFollowingId, followingId)
                .one();
        return R.success(userRelation, "查询用户关系信息成功");
    }

    /**
     * 修改用户关系信息
     * @param userRelation
     * @return
     */
    @PostMapping("/update")
    public R<UserRelation> update(@RequestBody UserRelation userRelation){
        log.info("修改的用户关系信息为{}", userRelation);
        userRelationService.updateById(userRelation);
        return R.success(userRelation,"修改用户关系信息成功");
    }

}


